﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xts
{
    namespace Util
    {
        public class XSerialGenerator
        {
            protected long x_startNum;
            protected string x_prefix;
            protected long x_accmValue;
            protected Object x_lockObj;

            public XSerialGenerator()
            {
                x_startNum = 0;
                x_prefix = string.Empty;
                x_accmValue = 0;
                x_lockObj = new Object();
            }

            public XSerialGenerator(long startNum, string prefix)
            {
                x_startNum = startNum;
                x_prefix = prefix;
                x_accmValue = 0;
                x_lockObj = new Object();
            }

            public long SerialNum()
            {
                return x_startNum + x_accmValue++;
            }

            public string XSerialStr()
            {
                return x_prefix + Convert.ToString(SerialNum());
            }

            public long TreadSafeSerialNum()
            {
                lock (x_lockObj)
                {
                    return x_startNum + x_accmValue++;
                }
            }

            public string TreadSafeSerialStr()
            {
                lock (x_lockObj)
                {
                    return x_prefix + Convert.ToString(SerialNum());
                }
            }
        }
    }
}
